package rs.belex.belexdroid.services.impl.ksoap2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import rs.belex.belexdroid.services.interfaces.IServiceClient;
import android.util.Log;

public abstract class ServiceClientKsoap2Impl implements IServiceClient {

	private static String TAG = LoginKsoap2Impl.class.getSimpleName();

	/**Parser implementation*/
	Date dateParser(String dateResponse){
		//Unsuccessfully tried to parse date response with yyyy-MM-dd'T'HH:mm:ss.SSSZ
		//there are solutions with substrings like below
		//representation of calendar date (yyyy-MM-dd)
		String calendar = dateResponse.substring(0, 10);
		//representation of time (HH:mm:ss)
		String time = dateResponse.substring(11, 19);
		
		SimpleDateFormat format = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = format.parse(calendar + " " + time);
		} catch (ParseException e) {
			Log.e(TAG, "Error while parsing date with SimpleDateFormat");
			e.printStackTrace();
		}
		//Because ksoap changes time zone this simple config. is needed
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, 1);
		date = cal.getTime();
		
		return date;
	}
}
